<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" draggable :show-fullscreen="true" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="120px" label-position="left">
            <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" clearable></el-input>
        	</el-form-item>
            <el-form-item label="类型" prop="type">
                <el-radio v-model="form.type" v-for="(item,index) in type_option" :key="index" :label="index">{{item}}</el-radio>
        	</el-form-item>
            <el-form-item label="类型" prop="type_status">
                <el-radio v-model="form.type_status" v-for="(item,index) in type_status_option" :key="index" :label="index">{{item}}</el-radio>
        	</el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio v-model="form.status" v-for="(item,index) in status_option" :key="index" :label="index">{{item}}</el-radio>
        	</el-form-item>
            <el-form-item label="适用商品" prop="goods_ids">
                <el-input v-model="form.goods_ids" clearable></el-input>
        	</el-form-item>
            <el-form-item label="券面额" prop="jian">
                <el-input-number v-model="form.jian"></el-input-number>
        	</el-form-item>
            <el-form-item label="每次可抵扣(次卡)" prop="deduct">
                <el-input-number v-model="form.deduct"></el-input-number>
        	</el-form-item>
            <el-form-item label="返回贝壳" prop="return_shell">
                <el-input-number v-model="form.return_shell"></el-input-number>
        	</el-form-item>
            <el-form-item label="消费门槛" prop="man">
                <el-input-number v-model="form.man"></el-input-number>
        	</el-form-item>
            <el-form-item label="库存" prop="stock">
                <el-input-number v-model="form.stock"></el-input-number>
        	</el-form-item>
            <el-form-item label="每人限制" prop="limit">
                <el-input-number v-model="form.limit"></el-input-number>
        	</el-form-item>
            <el-form-item label="领取周期" prop="gettime">
                <el-date-picker v-model="form.gettime" type="datetime"></el-date-picker>
        	</el-form-item>
            <el-form-item label="有效期天" prop="day">
                <el-input-number v-model="form.day"></el-input-number>
        	</el-form-item>
            <el-form-item label="小时" prop="hour">
                <el-input-number v-model="form.hour"></el-input-number>
        	</el-form-item>
            <el-form-item label="分钟" prop="min">
                <el-input-number v-model="form.min"></el-input-number>
        	</el-form-item>
            <el-form-item label="复购次数" prop="repeat">
                <el-input-number v-model="form.repeat"></el-input-number>
        	</el-form-item>
            <el-form-item label="描述" prop="description">
                <el-input v-model="form.description" clearable></el-input>
        	</el-form-item>
            <el-form-item label="使用开始时间" prop="usetimestart">
                <el-input-number v-model="form.usetimestart"></el-input-number>
        	</el-form-item>
            <el-form-item label="使用结束时间" prop="usetimeend">
                <el-input-number v-model="form.usetimeend"></el-input-number>
        	</el-form-item>
            <el-form-item label="店铺ID" prop="store_id">
                <el-input-number v-model="form.store_id"></el-input-number>
        	</el-form-item>
            <el-form-item label="赠送" prop="give_status">
                <el-radio v-model="form.give_status" v-for="(item,index) in give_status_option" :key="index" :label="index">{{item}}</el-radio>
        	</el-form-item>
            <el-form-item label="领取状态" prop="get_status">
                <el-radio v-model="form.get_status" v-for="(item,index) in get_status_option" :key="index" :label="index">{{item}}</el-radio>
        	</el-form-item>
            <el-form-item label="混合付" prop="mix_status">
                <el-radio v-model="form.mix_status" v-for="(item,index) in mix_status_option" :key="index" :label="index">{{item}}</el-radio>
        	</el-form-item>
            <el-form-item label="场景" prop="scene">
                <el-select v-model="form.scene" clearable placeholder="请选择场景">
            <el-option v-for="(item,index) in scene_option" :key="index" :label="item" :value="index"></el-option>
        </el-select>
        	</el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false" >取 消</el-button>
            <el-button v-if="mode!='read'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
    
    export default {
        emits: ['success', 'closed'],
        
        data() {
            return {
                mode: "create",
                titleMap: {
                    read: '查看',
                    create: '新增',
                    update: '编辑',
                },
                visible: false,
                isSaveing: false,
                form: {
                    id: 0,
					name: '',
					type: '',
					type_status: '',
					status: '',
					goods_ids: '',
					jian: '',
					deduct: '',
					return_shell: '',
					man: '',
					stock: '',
					limit: '',
					gettime: '',
					day: '',
					hour: '',
					min: '',
					repeat: '',
					description: '',
					usetimestart: '',
					usetimeend: '',
					store_id: '',
					give_status: '',
					get_status: '',
					mix_status: '',
					scene: '',
                },
                rules: {
                },
				type_option:{"cash":"代金券","discount":"折扣券"},
				type_status_option:{"1":"满减卷","2":"复购卷","3":"分享卷","4":"抵扣卷","5":"引流卷","6":"商品卷","7":"领劵中心卷"},
				status_option:{"up":"上架","halt":"暂停","down":"下架"},
				give_status_option:{"1":"不支持","2":"支持"},
				get_status_option:{"1":"店铺指定","2":"都可领取"},
				mix_status_option:{"1":"不支持","2":"支持"},
				scene_option:{"current":"通用","auction":"拍卖","groupon":"拼团","rice":"米市","direct":"直接付","scan":"扫码付"},
            }
        },
        methods: {
            open(mode='create'){
                this.mode = mode;
                this.visible = true;
                return this
            },
            submit(){
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res = await this.$API.coupons[this.mode].post(this.form);
                        this.isSaveing = false;
                        if(res.code == 200){
                            this.$emit('success', this.form, this.mode)
                            this.visible = false;
                            this.$message.success(res.message)
                        }else{
                            this.$alert(res.message, "提示", {type: 'error'})
                        }
                    }else{
                        return false;
                    }
                })
            },
            setData(data){
                this.$TABLE.assign(this,data);
            }
        }
    }
</script>